** Data Operations
cd "/Users/mmoral/Dropbox/--Tamamlanan Çalışma ve Yayınlar/--Elections and Public Opinion in Turkey_Through the Prism of the 2018 Elections/-Replication/Politics as (Un)Usual? An Overview of the June 2018 Presidential and Parliamentary Elections in Turkey"

* June 2018 Parliamentary and Presidential Elections (Electoral District-Level) 
use "/Users/mmoral/Dropbox/My Mac (MMs-Mac.local)/Downloads/YSK - Turkish Election Data/YSK 2018C_L2.dta", clear
replace district="EYÜP" if district=="EYÜPSULTAN" & province=="İSTANBUL"
save "YSK 2018C_L2_972.dta", replace

use "/Users/mmoral/Dropbox/My Mac (MMs-Mac.local)/Downloads/YSK - Turkish Election Data/YSK 2018P_L2.dta", clear
replace district="EYÜP" if district=="EYÜPSULTAN" & province=="İSTANBUL"
save "YSK 2018P_L2_972.dta", replace

* November 2015 Parliamentary Elections (Precinct-Level) 
use "/Users/mmoral/Dropbox/My Mac (MMs-Mac.local)/Downloads/YSK - Turkish Election Data/YSK 2015N_L3.dta", clear
foreach mah in CUMHURİYET GAZİ İSTİKAMET KOYUNCU SELÇUK TÜRKİSTAN ZAFER{
	replace district="SULTANHANI" if precinct=="SULTANHANI-`mah' MAH." & district=="AKSARAY MERKEZ"	
	replace precinct="`mah' MAH." if precinct=="SULTANHANI-`mah' MAH." & district=="SULTANHANI"
}
replace district="SULTANHANI" if precinct=="YEŞİLTÖMEK KÖYÜ" & district=="AKSARAY MERKEZ"
foreach mah in CUMHURİYET SELİMİYE UZUNYALI{
	replace district="KEMALPAŞA" if precinct=="KEMALPAŞA-`mah' MAH." & district=="HOPA"	
	replace precinct="`mah' MAH." if precinct=="KEMALPAŞA-`mah' MAH." & district=="KEMALPAŞA"
}
foreach koy in AKDERE ÇAMURLU DEREİÇİ GÜMÜŞDERE KARAOSMANİYE KAYA KAZİMİYE KÖPRÜCÜ LİMAN OSMANİYE SARP ÜÇKARDEŞ{
	replace district="KEMALPAŞA" if precinct=="`koy' KÖYÜ" & district=="HOPA"	
}
collapse (sum) ballotbox_num eligible voter valid invalid akp chp mhp hdp sp other, by(province district)
save "YSK 2015N_L2_972.dta", replace

* June 2015 Parliamentary Elections (Precinct-Level) 
use "/Users/mmoral/Dropbox/My Mac (MMs-Mac.local)/Downloads/YSK - Turkish Election Data/YSK 2015J_L3.dta", clear
foreach mah in CUMHURİYET GAZİ İSTİKAMET KOYUNCU SELÇUK TÜRKİSTAN ZAFER{
	replace district="SULTANHANI" if precinct=="SULTANHANI-`mah' MAH." & district=="AKSARAY MERKEZ"	
	replace precinct="`mah' MAH." if precinct=="SULTANHANI-`mah' MAH." & district=="SULTANHANI"
}
replace district="SULTANHANI" if precinct=="YEŞİLTÖMEK KÖYÜ" & district=="AKSARAY MERKEZ"
foreach mah in CUMHURİYET SELİMİYE UZUNYALI{
	replace district="KEMALPAŞA" if precinct=="KEMALPAŞA-`mah' MAH." & district=="HOPA"	
	replace precinct="`mah' MAH." if precinct=="KEMALPAŞA-`mah' MAH." & district=="KEMALPAŞA"
}
foreach koy in AKDERE ÇAMURLU DEREİÇİ GÜMÜŞDERE KARAOSMANİYE KAYA KAZİMİYE KÖPRÜCÜ LİMAN OSMANİYE SARP ÜÇKARDEŞ{
	replace district="KEMALPAŞA" if precinct=="`koy' KÖYÜ" & district=="HOPA"	
}
collapse (sum) ballotbox_num eligible voter valid invalid akp chp mhp hdp sp other, by(province district)
save "YSK 2015J_L2_972.dta", replace

* August 2014 Presidential Elections (Precinct-Level) 
use "/Users/mmoral/Dropbox/My Mac (MMs-Mac.local)/Downloads/YSK - Turkish Election Data/YSK 2014C_L3.dta", clear
foreach mah in CUMHURİYET GAZİ İSTİKAMET KOYUNCU SELÇUK TÜRKİSTAN ZAFER{
	replace district="SULTANHANI" if precinct=="SULTANHANI-`mah' MAH." & district=="AKSARAY MERKEZ"	
	replace precinct="`mah' MAH." if precinct=="SULTANHANI-`mah' MAH." & district=="SULTANHANI"
}
replace district="SULTANHANI" if precinct=="YEŞİLTÖMEK KÖYÜ" & district=="AKSARAY MERKEZ"
foreach mah in CUMHURİYET SELİMİYE UZUNYALI{
	replace district="KEMALPAŞA" if precinct=="KEMALPAŞA-`mah' MAH." & district=="HOPA"	
	replace precinct="`mah' MAH." if precinct=="KEMALPAŞA-`mah' MAH." & district=="KEMALPAŞA"
}
foreach koy in AKDERE ÇAMURLU DEREİÇİ GÜMÜŞDERE KARAOSMANİYE KAYA KAZİMİYE KÖPRÜCÜ LİMAN OSMANİYE SARP ÜÇKARDEŞ{
	replace district="KEMALPAŞA" if precinct=="`koy' KÖYÜ" & district=="HOPA"	
}
replace district="KAZAN" if district=="KAHRAMANKAZAN" & province=="ANKARA"
collapse (sum) ballotbox_num eligible voter valid invalid emi rte sd, by(province district)
save "YSK 2014_L2_972.dta", replace

* For the Maps
import excel "/Users/mmoral/Dropbox/My Mac (MMs-Mac.local)/Downloads/MAPS/Turkey_09.2018 - 972 Districts/District List_copy.xlsx", sheet("Sheet1") firstrow clear
rename (District_TU Province_TU) (district province)
replace district="KAZAN" if district=="KAHRAMANKAZAN"
save "Turkey_l2_v2.dta", replace

* Other Data Operations
clear all
loc exclude "province district"
foreach elec in 2018C 2018P 2015N 2015J 2014 {
use "YSK `elec'_L2_972.dta", clear
unab torename: _all
loc varlist: list torename - exclude
foreach var in `varlist' {
rename `var' `var'_`elec'
} 
save "YSK `elec'_v2.dta", replace
}

use "YSK 2018C_v2", clear
foreach elec in 2018P 2015N 2015J 2014 {
merge 1:1 province district using "YSK `elec'_v2.dta", nogen
}
merge 1:1 province district using "Turkey_l2_v2.dta", nogen
drop District_EU-Province_TL PCODE-Shape_Area

foreach elec in 2018C 2018P 2015N 2015J 2014 {
erase "YSK `elec'_L2_972.dta"
erase "YSK `elec'_v2.dta"
}
erase "Turkey_l2_v2.dta"

* Replication Data for Descriptive Analyses
save "Replication Data.dta", replace
